package com.redoor.app.stake.basicdata.service.impl;

import com.redoor.app.stake.auth.commons.DatabaseServiceStatus;
import com.redoor.app.stake.auth.model.BaseUser;
import com.redoor.app.stake.auth.service.BaseUserService;
import com.redoor.app.stake.basicdata.dao.BaseEquipmentInfoMapper;
import com.redoor.app.stake.basicdata.dao.BaseVendorInfoMapper;
import com.redoor.app.stake.basicdata.model.BaseEquipmentInfo;
import com.redoor.app.stake.basicdata.model.BaseVendorInfo;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseVendorInfoActionBean;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseVendorInfoCriteria;
import com.redoor.app.stake.basicdata.service.BaseVendorInfoService;
import com.redoornetwork.framework.mybatis.service.AbstractService;
import com.yotouch.app.stake.model.Vendor;
import org.omg.CORBA.INTERNAL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;

import java.util.*;

/**
 * Created by wangjunlei on 2018-03-12 17:30:28.
 */
@Service
public class BaseVendorInfoServiceImpl extends AbstractService<BaseVendorInfo, String> implements BaseVendorInfoService {

    private Logger logger = LoggerFactory.getLogger(BaseVendorInfoServiceImpl.class);

    @Autowired
    private BaseVendorInfoMapper baseVendorInfoMapper;
    // 用户业务层
    @Autowired
    private BaseUserService baseUserService;
    // (桩)设备型号mapper
    @Autowired
    private BaseEquipmentInfoMapper baseEquipmentInfoMapper;



    @Override
    public BaseVendorInfo getVendorByParams(BaseVendorInfoCriteria criteria) {
        if (criteria==null){
            logger.info("BaseVendorInfoServiceImpl-->getVendorByParams：查询条件不能为空!");
            return null;
        }
        List<BaseVendorInfo> baseVendorInfos = baseVendorInfoMapper.queryVendorByParams(criteria);
        BaseVendorInfo baseVendorInfo = (baseVendorInfos!=null && baseVendorInfos.size()>0)?baseVendorInfos.get(0):null;
        return baseVendorInfo;
    }

    @Override
    public int updateStatus(String uuid) {
        BaseUser loginUser = this.baseUserService.getShiroUser(null);
        BaseVendorInfo baseVendorInfo = new BaseVendorInfo();
        baseVendorInfo.setUuid(uuid);
        baseVendorInfo.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_DEL_STATUS.getCode()));
        baseVendorInfo.setUpdateruuid(loginUser.getUuid());
        baseVendorInfo.setUpdatedat(new Date());
        //修改厂商状态为删除
        int status = this.baseVendorInfoMapper.updateByPrimaryKeySelective(baseVendorInfo);

        BaseEquipmentInfo baseEquipmentInfo = new BaseEquipmentInfo();
        baseEquipmentInfo.setUpdatedat(baseVendorInfo.getUpdatedat());
        baseEquipmentInfo.setUpdateruuid(baseVendorInfo.getUpdateruuid());
        baseEquipmentInfo.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_DEL_STATUS.getCode()));
        //创建条件
        Condition condition=new Condition(BaseEquipmentInfo.class);
        //创建查询条件
        condition.createCriteria().andCondition("vendorUuid ='" + baseVendorInfo.getUuid() + "'");
            //根据查询条件修改
            status += this.baseEquipmentInfoMapper.updateByConditionSelective(baseEquipmentInfo,condition);
        return status;
    }

    @Override
    public int updateVendorInfo(BaseVendorInfoCriteria baseVendorInfoCriteria) {
        BaseUser loginUser = this.baseUserService.getShiroUser(null);
        BaseVendorInfo baseVendorInfo = new BaseVendorInfo();
        baseVendorInfo.setUpdatedat(new Date());
        baseVendorInfo.setUpdateruuid(loginUser.getUuid());
        baseVendorInfo.setUuid(baseVendorInfoCriteria.getUuid());
        baseVendorInfo.setName(baseVendorInfoCriteria.getName());
        baseVendorInfo.setTel(baseVendorInfoCriteria.getTel());
        baseVendorInfo.setVendorno(baseVendorInfoCriteria.getVendorNo());
        return this.baseVendorInfoMapper.updateByPrimaryKeySelective(baseVendorInfo);
    }

    @Override
    public int saveVendorInfo(BaseVendorInfoCriteria baseVendorInfoCriteria) {
        BaseUser loginUser = this.baseUserService.getShiroUser(null);
        BaseVendorInfo baseVendorInfo = new BaseVendorInfo();
        baseVendorInfo.setCreatedat(new Date());
        baseVendorInfo.setCreatoruuid(loginUser.getUuid());
        baseVendorInfo.setUpdatedat(baseVendorInfo.getCreatedat());
        baseVendorInfo.setUpdateruuid(baseVendorInfo.getCreatoruuid());
        baseVendorInfo.setName(baseVendorInfoCriteria.getName());
        baseVendorInfo.setVendorno(baseVendorInfoCriteria.getVendorNo());
        baseVendorInfo.setTel(baseVendorInfoCriteria.getTel());
        baseVendorInfo.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_NORMAL_STATUS.getCode()));
        baseVendorInfo.setUuid(UUID.randomUUID().toString());
        return this.baseVendorInfoMapper.insert(baseVendorInfo);
    }

    @Override
    public List<BaseVendorInfo> qryVendorInfo(BaseVendorInfoActionBean baseVendorInfoActionBean) {
        return this.baseVendorInfoMapper.qryVendorInfo(baseVendorInfoActionBean);
    }

    @Override
    public Long qryVendorInfoTotal(BaseVendorInfoActionBean baseVendorInfoActionBean) {
        return this.baseVendorInfoMapper.qryVendorInfoTotal(baseVendorInfoActionBean);
    }

    @Override
    public Map<String, Integer> qryVendorInfoByNameOrVendorNo(String name, String vendorNo) {
        Map<String, Integer> map = new HashMap<String, Integer>(2);
        BaseVendorInfo baseVendorInfo = new BaseVendorInfo();
        baseVendorInfo.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_NORMAL_STATUS.getCode()));
        baseVendorInfo.setName(name);
        //查询名称是否已存在
        int nameStatus = this.baseVendorInfoMapper.selectCount(baseVendorInfo);
        //清空查询条件中名称查询
        baseVendorInfo.setName(null);
        baseVendorInfo.setVendorno(vendorNo);
        //查询厂商编号是否存在
        int vendorNoStatus = this.baseVendorInfoMapper.selectCount(baseVendorInfo);
        map.put("nameStatus", nameStatus);
        map.put("vendornoStatus", vendorNoStatus);
        return map;
    }

    @Override
    public List<Vendor> qryVendorInfoToHSF() {
        return this.baseVendorInfoMapper.qryVendorInfoToHSF();
    }

    @Override
    public Vendor qryVendorInfoByVendorUuidToHSF(String vendorUuid) {
        return this.baseVendorInfoMapper.qryVendorInfoByVendorUuidToHSF(vendorUuid);
    }
}
